use odin_net::node::{NodeNet, NodeId};
use tokio::sync::mpsc::UnboundedReceiver;
mod probe;

pub struct Node {
    net: NodeNet,
}

impl Node {
    pub fn new(self_id: NodeId) -> Self {
        Self {
            net : NodeNet::new(self_id),
        }
    }

    pub fn id(&self) -> &NodeId {
        &self.net.id()
    }

    pub async fn listen(&mut self, addr: &str) -> tokio::io::Result<&mut Self> {
        self.net.listen(addr).await?;

        Ok(self)
    }

    pub async fn connect(&mut self, addr: &str) -> &mut Self {
        self.net.connect(addr).await;

        self
    }

    pub async fn start_service<S>(&self, mut service: S) -> tokio::io::Result<&Self> 
        where 
            S: odin_service::Service<Node, UnboundedReceiver<(NodeId, u32, Vec<u8>)>> + odin_service::Named + Send + 'static {

        let channel = self.net.add_channel(S::NAME).await?;
        tokio::spawn(async move {
            let mut channel = channel;
            if let Err(e) = service.serve(self, channel).await {
                
            }
        });

        Ok(self)
    }

}